<template>
  <div class="login-container">
    <div class="login-title">基于vue-element-admin的后台管理系统</div>
    <div class="admin-login-background">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div class="title-container">
          <h3 class="title">后台登录</h3>
        </div>

        <el-form-item>
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="username" v-model="loginForm.username" placeholder="请输入账号" name="username" type="text" tabindex="1" auto-complete="on" />
        </el-form-item>

        <el-form-item>
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom: 22px;" @click.native.prevent="handleLogin">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsernameOld } from '@/utils/validate'
import '@/utils/particleground'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsernameOld(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请输入至少6位数字'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    // 粒子线条背景
    this.$('.login-container').particleground({
      dotColor: '#7ec7fd',
      lineColor: '#7ec7fd'
    })
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            username: this.loginForm.username,
            password: this.$md5(this.loginForm.password)
          }
          this.$store
            .dispatch('user/login', params)
            .then(() => {
              this.$router.push({ path: '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/styles/variables.scss';
$bg: $menuActiveText;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  .login-title {
    font-size: 28px;
    color: #fff;
    position: absolute;
    top: 24%;
    width: 100%;
    text-align: center;
  }
  .admin-login-background {
    width: 400px;
    height: 300px;
    position: absolute;
    left: calc(50% - 200px);
    top: 34%;
  }

  .login-form {
    background-color: #fff;
    border: 1px solid #fff;
    border-radius: 3px;
    padding: 14px 20px;
    box-shadow: 0 0 8px #eeeeee;
  }
  .el-form-item {
    position: relative;
  }

  .svg-container {
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 100;
    padding: 0 12px;
  }

  .title-container {
    position: relative;
    text-align: center;
    letter-spacing: 2px;
    padding: 10px 0 25px 0;
    .title {
      font-size: 24px;
      margin: 0;
      font-weight: normal;
    }
  }

  .show-pwd {
    position: absolute;
    right: 12px;
    top: 3px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.login-container ::v-deep .el-input__inner {
  padding-left: 38px;
}
</style>
